package Solution125_150;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution150 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for(int i=0;i<tokens.length;i++){
            int num1,num2;
            switch (tokens[i]){
                case "+":
                    num1=stack.pop();
                    num2=stack.pop();
                    stack.push(num1+num2);
                    break;
                case "-":
                    num1=stack.pop();
                    num2=stack.pop();
                    stack.push(-num1+num2);
                    break;
                case "*":
                    num1=stack.pop();
                    num2=stack.pop();
                    stack.push(num1*num2);
                    break;
                case "/":
                    num1=stack.pop();
                    num2=stack.pop();
                    stack.push(num2/num1);
                    break;
                default:
                    stack.push(Integer.parseInt(tokens[i]));
                    break;

            }
        }
        return stack.pop();
    }

    @Test
    public void test(){
        String[] list=new String[]{"2","1","+","3","*"};

        System.out.println(evalRPN(list));
    }
}
